<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: lkx
  Date: 2020/11/20
  Time: 21:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="conn.jsp"%>
<html>
<head>
    <title>后台用户登录接口</title>
</head>
<body>


<%
     //第一步：获取参数 用户名和密码
    String yhm=request.getParameter("cs_yhm");
    String mm=request.getParameter("cs_mm");
    //out.print("<br>用户名="+yhm+"|密码="+mm);
    //out.print("<script>alert('"+yhm+"')</script>");

    //第二步：判断参数是否为空
    if (yhm==null||yhm==""||mm==""||mm==null){
        out.print("<br>请输入用户名和密码");
    }else {
        //第三步：读取数据库判断用户名和密码是否正确
        Statement stmt=null;
        ResultSet rs=null;
        String sql="";
        sql= "select * from quanxian_yonghu ";
        sql=sql+ " where user_name='"+yhm+"'" ;
        sql=sql + " and user_password='"+mm+"'";
        stmt = conn.createStatement();
        rs=stmt.executeQuery(sql);
        if (rs.next()){
            //正确：弹出提示，跳转到后台首页
            out.print("<br>用户名和密码正确");
            Cookie u_id=new Cookie("u_id",rs.getString("id"));
            u_id.setMaxAge(60*24*24);
            response.addCookie(u_id);
            //setMaxAge()过期时间
            //setMaxAge（）设置值大于0，将cookie存储于本地磁盘，过期后删除
            // setMaxAge）设置值小于0，cookie不会保存于本地，浏览器会话结束后，将会删除，经过在Mac上的chorme测试，需要结束进程后cookie才会从内存中删除
            // setMaxAge（）设置值等于0，立即删除cookie

            Cookie u_fzid=new Cookie("u_id",rs.getString("fenzu_id"));
            u_fzid.setMaxAge(60*24*24);
            response.addCookie(u_fzid);

            Cookie u_name=new Cookie("u_name",yhm);
            u_name.setMaxAge(60*24*24);
            response.addCookie(u_name);

            out.print("<script>alert('登录成功');window.parent.location='/Houtai/manager/admin_index.jsp'</script>");
        }else {
            //错误：弹出提示
            out.print("<br>用户名和密码错误");
            out.print("<script>alert('用户名和密码错误')</script>");
        }

    }

%>

</body>
</html>
